package com.zhongli.devplatform.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongli.devplatform.bean.PageVO;
import com.zhongli.devplatform.dto.sys.SystemParameterDTO;
import com.zhongli.devplatform.mapper.sys.ReadSystemParameterMapper;
import com.zhongli.devplatform.po.sys.ReadSystemParameterPO;
import com.zhongli.devplatform.query.sys.SystemParameterQuery;
import com.zhongli.devplatform.service.imp.sys.ReadSystemParameterService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.SystemParameterVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class ReadSystemParameterServiceImpl implements ReadSystemParameterService {

    @Autowired
    private ReadSystemParameterMapper readSystemParameterMapper;

    @Autowired
    private CacheManager cacheManager;

    @Value("${dev-platform.cache-name}")
    private String cacheName;

    @Override
    public PageVO<SystemParameterVO> listPage(SystemParameterQuery query) {
        QueryWrapper queryWrapper = new QueryWrapper<ReadSystemParameterPO>();
        if (!StringUtils.isBlank(query.getParamName())) {
            queryWrapper.eq("param_key", query.getParamName());
            queryWrapper.or(true).like("param_name", query.getParamName());
        }

        Page page = readSystemParameterMapper.selectPage(query, queryWrapper);
        PageVO newPage = PageVO.convert(page);
        newPage.setRecords(BeanCopierUtil.copy(page.getRecords(), SystemParameterVO.class));
        return newPage;
    }

    @Override
    public void save(SystemParameterDTO SystemParameterDTO) {
        ReadSystemParameterPO readSystemParameterPO = BeanCopierUtil.copy(SystemParameterDTO, ReadSystemParameterPO.class);
        if (readSystemParameterPO.getId() == null) {
            readSystemParameterMapper.insert(readSystemParameterPO);
        } else {
            readSystemParameterMapper.updateById(readSystemParameterPO);
        }
    }

    @Override
    public void delete(List<Long> ids) {
        readSystemParameterMapper.deleteBatchIds(ids);
    }

    @Override
    public Boolean isExist(SystemParameterQuery query) {
        QueryWrapper queryWrapper = new QueryWrapper<ReadSystemParameterPO>();
        queryWrapper.eq("param_key", query.getParamKey());
        if (query.getId() != null) {
            queryWrapper.ne("id", query.getId());
        }
        return readSystemParameterMapper.selectCount(queryWrapper) > 0;

    }

    @Override
    @PostConstruct
    public void putCache() {

        List<ReadSystemParameterPO> readSystemParameterPOList = readSystemParameterMapper.selectList(null);
        for (ReadSystemParameterPO readSystemParameterPO : readSystemParameterPOList) {
            cacheManager.getCache(cacheName + CACHE_KEY).put(readSystemParameterPO.getParamKey(), readSystemParameterPO.getParamValue());
        }
    }

    @Override
    public String getSystemParameter(String key) {
        String s = cacheManager.getCache(cacheName + CACHE_KEY).get(key, String::new);
        System.out.println(s);
        return cacheManager.getCache(cacheName + CACHE_KEY).get(key, String::new);

    }
}
